'use strict';

/**
 * @ngdoc function
 * @name youaBiDashboardAngularApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the youaBiDashboardAngularApp
 */
angular.module('youaBiDashboardAngularApp')
  .controller('MainCtrl', function($scope, $http, $location) {
    var username = window.localStorage.getItem('username');
    var csrftoken = window.localStorage.getItem('csrftoken');

    if (!username || !csrftoken) {
      return $location.path('/login')
    }

    $http({
      method: 'POST',
      url: '/user/checkUserLogin',
      params: {
        username: username,
        csrftoken: csrftoken
      }
    }).then(function successCallback(response) {
      var result = response.data.result;
      if (!result) {
        window.localStorage.removeItem('username')
        window.localStorage.removeItem('csrftoken')

        return $location.path('/login')
      }
    })

    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];

    $scope.logout = function () {
      var username = window.localStorage.getItem('username');

      $http({
        method: 'POST',
        url: '/user/logout',
        params: {
          username: username
        }
      }).then(function successCallback(response) {
        var result = response.data.result;
        if (result) {
          window.localStorage.removeItem('username')
          window.localStorage.removeItem('csrftoken')

          return $location.path('/login')
        }
      })
    }

    $scope.template = {
      sidebar: "views/sidebar.html",
      header: "views/header.html"
    }

    $scope.summery = {}

    $http({
      method: 'GET',
      url: '/order/summery'
    }).then(function successCallback(response) {
      $scope.summery = response.data.result.summery
    }, function errorCallback(response) {

    });

    $scope.chart = {
      labels: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
      series: ['订单数', '支付订单数'],
      data: [],
      summery: {}
    }

    $scope.kpis = [];


    $http({
      method: 'GET',
      url: '/order/chart',
      params: {
        period: 'week',
        timestamp: Date.now()
      }
    }).then(function successCallback(response) {

      var details = response.data.result ? response.data.result.detail : null;
      if (details) {
        var tmp = _.filter(details, function(value, key, list) {
          return value.terminal_type === 'TOTAL' && value.cps_channel === 'TOTAL';
        });

        if (tmp) {
          var orderNumArr = _.pluck(tmp, 'order_num');
          var paidOrderNumArr = _.pluck(tmp, 'paid_order_num');
          $scope.chart.data.push(orderNumArr, paidOrderNumArr);
        }

        var summery = response.data.result ? response.data.result.summery : null;
        if (summery) {
          var tmp = _.findWhere(summery, {
            terminal_type: 'TOTAL',
            'cps_channel': 'TOTAL'
          });

          $scope.chart.summery.orderNum = tmp.order_num;
          $scope.chart.summery.paidOrderNum = tmp.paid_order_num;
          $scope.chart.summery.paidAmount = tmp.paid_payment_amount;
        }

      }
    }, function errorCallback(response) {

    });


    $http({
      method: 'GET',
      url: '/kpi/list',
      params: {
        period: 'week',
        timestamp: Date.now()
      }
    }).then(function successCallback(response) {

      var results = response.data.result;
      var nresults = _.sortBy(results, function(value, key, list) {
        return -value.paid_payment_amount
      });

      $scope.kpis = nresults.slice(0, 5);

    }, function errorCallback(response) {

    });

  });